package com.augurit.gzcgwsp.sendNotice.web;

import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.form.ResultForm;
import com.augurit.common.util.ExtUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.sendNotice.convert.YwWebserviceInfoConvert;
import com.augurit.gzcgwsp.sendNotice.entity.YwWebserviceInfo;
import com.augurit.gzcgwsp.sendNotice.service.IYwWebserviceInfoService;
import com.augurit.gzcgwsp.sendNotice.web.form.YwWebserviceInfoForm;
import com.augurit.gzcgwsp.state.util.RequestUtil;

@SuppressWarnings("serial")
@Results( {
		@Result(name = CrudActionSupport.RELOAD, location = "yw-webservice-info.action", type = "redirect"),
		@Result(name = "toList", location = "/gzcgwsp/ywcmn/YwWebserviceInfo/listYwWebserviceInfo.jsp"),
		@Result(name = "info", location = "/gzcgwsp/ywcmn/YwWebserviceInfo/YwWebserviceInfo.jsp") })
public class YwWebserviceInfoAction extends
		CrudActionSupport<YwWebserviceInfoForm> {

	private YwWebserviceInfoForm form;
	private Long id;
	private Page<YwWebserviceInfoForm> page;
	@Autowired
	private IYwWebserviceInfoService ywWebserviceInfoService;

	@Override
	public String delete() throws Exception {
		ywWebserviceInfoService.delete(id);
		return null;
	}

	@Override
	public String deleteMore() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {

		return "toList";
	}

	@Override
	public String input() throws Exception {

		return INPUT;
	}

	public String info() throws Exception {
//		System.out.println("id ======================= " + id);
		if (id != null) {
			form = ywWebserviceInfoService.get(id);
		} else {
			form = new YwWebserviceInfoForm();
		}
		return "info";
	}

	/**
	 * 查询数据列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getData() throws Exception {
		Map map = RequestUtil.getMapByRequest(getRequest());
		String isSended = (String)map.get("isSended");
		String produceDate = (String)map.get("produceDate");
		String acceptNo = (String)map.get("acceptNo");
		if (page == null) {
			page = new Page(15);
		}
		ExtUtils.initPageFromExtGridParam(getRequest(), page);
		page = ywWebserviceInfoService.searchPage(page, map);

		renderText(Struts2Utils.getJsonString(page,
				DateUtils.YYYY_MM_DD_HH_MM_SS));
		return null;
	}

	public String saveWebserviceInfo() throws Exception {
		try {
			String callTimes = this.getParameter("callTimes");
			YwWebserviceInfo entity = new YwWebserviceInfo();
			YwWebserviceInfoConvert.convertFormToVo(form, entity);
			ywWebserviceInfoService.save(entity);
			this.renderJson(new ResultForm(true));
		} catch (RuntimeException e) {
			this.renderJson(new ResultForm(false));
			e.printStackTrace();
		}

		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			form = ywWebserviceInfoService.get(id);
		} else
			form = new YwWebserviceInfoForm();
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public YwWebserviceInfoForm getModel() {
		return form;
	}

	public Page<YwWebserviceInfoForm> getPage() {
		return page;
	}

	public void setPage(Page<YwWebserviceInfoForm> page) {
		this.page = page;
	}

	public YwWebserviceInfoForm getForm() {
		return form;
	}

	public void setForm(YwWebserviceInfoForm form) {
		this.form = form;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
